package com.hnevc.mytrafficdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.hnevc.mytrafficdemo.R;
import com.hnevc.mytrafficdemo.entity.BusStatus;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/22.
 */

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
    Context mContext;
    private Map<Integer,List<BusStatus>> mapBusStatusList;
    private Map<Integer ,String> mapName;

    public MyExpandableListViewAdapter(Context context, Map<Integer
            , List<BusStatus>> mapBusStatusList, Map<Integer, String> mapName) {
        mContext = context;
        this.mapBusStatusList = mapBusStatusList;
        this.mapName = mapName;
    }

    @Override
    public int getGroupCount() {
        return mapName.size();
    }

    @Override
    public int getChildrenCount(int i) {
        int busStattionId = (int) mapName.keySet().toArray()[i];
        return mapBusStatusList.get(busStattionId).size();
    }

    @Override
    public Object getGroup(int i) {
        return mapName.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        int busStattionId = (int) mapName.keySet().toArray()[i];
        return mapBusStatusList.get(busStattionId).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(mContext).inflate(R.layout.parent_list_item,null);
        TextView mTextView = (TextView) view.findViewById(R.id.id_tv_parent);
        int busStattionId = (int) mapName.keySet().toArray()[i];
        mTextView.setText(mapName.get(busStattionId));
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(mContext).inflate(R.layout.child_list_item,null);

        TextView mTvBusId = (TextView) view.findViewById(R.id.id_tv_bus_id);
        TextView mTvBusCapacity = (TextView) view.findViewById(R.id.id_tv_bus_capacity);
        TextView mTvBusTime = (TextView) view.findViewById(R.id.id_tv_bus_time);
        TextView mTvBusDistance = (TextView) view .findViewById(R.id.id_tv_bus_distance);
        int busStattionId = (int) mapName.keySet().toArray()[i];
        BusStatus busStatus = mapBusStatusList.get(busStattionId).get(i1);
        mTvBusId.setText(busStatus.getBusId()+"号车");
        mTvBusCapacity.setText("("+ busStatus.getCapacity()+"人)");
        mTvBusDistance.setText("距离站台"+ busStatus.getDistance()+"米");
        mTvBusTime.setText((int)( (busStatus.getDistance()/20000f)*60)+"分钟到达");
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}
